package com.musifind.activities;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.app.SearchManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.SearchView;

import com.musifind.adapters.TabsPagerAdapter;

public class SearchActivity extends FragmentActivity implements SearchView.OnQueryTextListener,
		SearchView.OnCloseListener, ActionBar.TabListener {

	private ViewPager viewPager;
	private TabsPagerAdapter mAdapter;
	private ActionBar actionBar;
	private String[] tabs = { "Şarkılar", "Albümler", "Sanatçılar" };
	private SearchView mSearchView;
	private String searchQuery;

	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);

		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
		setContentView(R.layout.activity_search);

		// activity starts as push old activity to left
		overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);

		BaseFragment.imgLogo = (ImageView) findViewById(R.id.imgBlankPage);

		// Initilization
		viewPager = (ViewPager) findViewById(R.id.pager);
		actionBar = getActionBar();
		mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

		viewPager.setAdapter(mAdapter);
		actionBar.setHomeButtonEnabled(true);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		for (String tab_name : tabs) {
			actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
		}

		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				actionBar.setSelectedNavigationItem(position);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});

		if (savedInstanceState != null) {
			actionBar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
		}

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// super.onSaveInstanceState(outState);
		outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.search_menu, menu);
		mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
		setupSearchView();
		return true;
	}

	private void setupSearchView() {
		mSearchView.setIconifiedByDefault(true);
		SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
		if (searchManager != null) {
			mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
		}
		mSearchView.setOnQueryTextListener(this);
		mSearchView.setOnCloseListener(this);
	}

	@Override
	public boolean onQueryTextChange(String newText) {
		return true;
	}

	public boolean onQueryTextSubmit(String query) {
		searchQuery = query;
		updateList();
		mSearchView.clearFocus();
		// TracksFragment.imgLogo.setVisibility(View.INVISIBLE);
		// AlbumsFragment.imgLogo.setVisibility(View.INVISIBLE);
		// ArtistsFragment.imgLogo.setVisibility(View.INVISIBLE);
		return false;
	}

	public boolean onClose() {
		// imgBlankPage.setVisibility(View.VISIBLE);
		return false;
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		viewPager.setCurrentItem(tab.getPosition());
		updateList();
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
	}

	private void updateList() {
		Fragment page = getSupportFragmentManager().findFragmentByTag(
				"android:switcher:" + R.id.pager + ":" + viewPager.getCurrentItem());
		if (page != null) {
			if (page instanceof BaseFragment) {
				BaseFragment baseFragment = (BaseFragment) page;
				if (searchQuery != null
						&& !searchQuery.isEmpty()
						&& (baseFragment.searchKey == null || baseFragment.searchKey.isEmpty() || !baseFragment.searchKey
								.equals(searchQuery))) {
					// baseFragment.getResults(searchQuery);
					baseFragment.callAsync(searchQuery);
					getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

				}
			}
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		BaseFragment.searchArrayTracks.clear();
		BaseFragment.searchArrayAlbums.clear();
		BaseFragment.searchArrayArtists.clear();
		BaseFragment.searchKey = null;
	}

}